package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.admin.model.*;
import com.dongdongshop.admin.vo.SpecOptionVO;
import com.dongdongshop.admin.vo.SpecVO;
import com.dongdongshop.admin.vo.TypeTemplateVO;
import com.dongdongshop.em.PageRequest;
import com.dongdongshop.em.PageResult;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.service.TypeTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TypeTemplateServiceImpl implements TypeTemplateService {
    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;
    @Autowired
    private TbSpecificationOptionMapper optionMapper;
    @Override
    public PageResult TypeSelect(PageRequest<TypeTemplateVO> typeVo) {
        TypeTemplateVO parms = typeVo.getParms();
        TbTypeTemplateExample tbTypeTemplateExample = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = tbTypeTemplateExample.createCriteria();
        if(StringUtils.isNotBlank(parms.getName())){
            criteria.andNameLike("%"+parms.getName()+"%");
        }
        PageHelper.startPage(typeVo.getPageNum(),typeVo.getPageSize());
        List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateMapper.selectByExample(tbTypeTemplateExample);
        PageInfo info = new PageInfo(tbTypeTemplates);
        List<TypeTemplateVO> collect = tbTypeTemplates.stream().map(l -> {
            TypeTemplateVO typeTemplateVO = new TypeTemplateVO();
            BeanUtils.copyProperties(l, typeTemplateVO);
            return typeTemplateVO;
        }).collect(Collectors.toList());
        return new PageResult(typeVo.getPageNum(),typeVo.getPageSize(),info.getTotal(), collect,5);
    }

    @Override
    public void add(TypeTemplateVO typeTemplateVO) {
        TbTypeTemplate type = new TbTypeTemplate();
        BeanUtils.copyProperties(typeTemplateVO,type);
        tbTypeTemplateMapper.insertSelective(type);
    }

    @Override
    public void update(TypeTemplateVO typeTemplateVO) {
        TbTypeTemplate type = new TbTypeTemplate();
        BeanUtils.copyProperties(typeTemplateVO,type);
        tbTypeTemplateMapper.updateByPrimaryKeySelective(type);
    }

    @Override
    public TypeTemplateVO getById(Long id) {
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);
        TypeTemplateVO VO = new TypeTemplateVO();
        BeanUtils.copyProperties(tbTypeTemplate, VO);

        //获取表中规格字符串
        String specIds = VO.getSpecIds();
        //将规格字符串转为集合
        JSONArray jsonArray = JSONObject.parseArray(specIds);
        //将集合用lamda表达式解析并把值付给相关字段
        List<SpecVO> specVOList = jsonArray.stream().map(obj -> {
            JSONObject jsonObject = (JSONObject) obj;
            Long id1 = Long.parseLong(jsonObject.get("id").toString());
            String text = (String) jsonObject.get("text");
            SpecVO specVO = new SpecVO();
            specVO.setId(id1);
            specVO.setSpecName(text);
            return specVO;
        }).collect(Collectors.toList());
        //查询全部规格数据
        List<TbSpecificationOption> optionList = optionMapper.selectByExample(null);
        //将查询出来的数据根据规格id分组,将统一规格的选项放到一个集合中,map的key是规格的id,value是那个集合
        Map<Long, List<SpecOptionVO>> collect = optionList.stream().map(option -> {
            SpecOptionVO specOptionVO = new SpecOptionVO();
            BeanUtils.copyProperties(option, specOptionVO);
            return specOptionVO;
        }).collect(Collectors.groupingBy(SpecOptionVO::getSpecId));
        //循环specVOList 从规格选项的分组中取出自己的规格选项,并设置给specVO的字段
        for (SpecVO specVO : specVOList) {
            List<SpecOptionVO> specOptionVOS = collect.get(specVO.getId());
            if (specOptionVOS==null){
                specOptionVOS = new ArrayList<>();
            }
            specVO.setList(specOptionVOS);
        }
        VO.setSpecVOList(specVOList);
        return VO;
    }

    @Override
    public void delete(Long[] ids) {
        TbTypeTemplateExample e = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = e.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        tbTypeTemplateMapper.deleteByExample(e);
    }

}
